<?php
namespace app\admin\service;

use app\model\DepartmentModel;
use app\model\not_del\DepartmentModel as NotDelDepartmentModel;
use app\model\DepartmentAdminModel;
use think\Service;

class UserService extends Service
{
    /**
     * 获取默认的头像
     * @param $argument
     * @return string
     */
    public function getDefaultAvatar($argument): string
    {
        return match ($argument) {
            'admin' => '/user_avatar/admin.png',
            'female' => '/user_avatar/female.png',
            'female_counselor' => '/user_avatar/female_counselor.png',
            'male_counselor' => '/user_avatar/male_counselor.png',
            default => '/user_avatar/male.png',
        };
    }

    /**
     * 获取部门权限完整id
     * @param $unit_id
     * @param $ids
     * @return array
     */
    public function getDepartmentAuthId($unit_id,$ids): array
    {
        $DepartmentModel = new NotDelDepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid')->where('unit_id','=', $unit_id)->select()->toArray();
        $department_id = [];
        foreach ($ids as $id) {
            $department_id = array_merge($department_id,getAllLowerLevelId($DepartmentList,$id));
        }

        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            if (!in_array($item['id'], $ids)) continue;
            if ($item['pid'] == 0) continue;
            $ids[] = $item['id'];
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        $ids[] = $items['id'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }

        return array_unique(array_merge($ids,$department_id));

    }

    /**
     * 获取部门的所有下级id
     * @param $unit_id
     * @param $ids
     * @return array
     */
    public function getDepartmentAllLowerLevelIds($unit_id,$ids): array
    {
        $DepartmentModel = new NotDelDepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid')->where('unit_id','=', $unit_id)->select()->toArray();
        $department_id = [];
        foreach ($ids as $id) {
            $department_id = array_merge($department_id,getAllLowerLevelId($DepartmentList,$id));
        }
        return array_unique(array_merge($ids,$department_id));
    }

    /**
     * 获取单个用户完整的所在部门-包含删除
     * @param $unit_id
     * @param $department_id
     * @return string
     */
    public function getNotDelDepartmentName($unit_id,$department_id): string
    {
        $DepartmentModel = new NotDelDepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid,name')->where(['unit_id' => $unit_id])->select()->toArray();
        foreach ($DepartmentList as $item) {
            if ($item['id'] != $department_id) continue;
            $name = $item['name'];
            $i = 0;
            $pid = $item['pid'];
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        $name = $items['name'].'_'.$name;
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        return $name??'--';
    }

    /**
     * 获取部门自身和上级所有id
     * @param $unit_id
     * @param $departmentIds
     * @return array
     */
    public function getDepartmentAllIds($unit_id,$departmentIds): array
    {
        $DepartmentModel = new DepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid')->where(['unit_id' => $unit_id])->select()->toArray();
        $ids = [];
        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            if (!in_array($item['id'], $departmentIds)) continue;
            if ($item['pid'] == 0) continue;
            $ids[] = $item['id'];
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        $ids[] = $items['id'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        return array_unique(array_merge($ids,$departmentIds));
    }

    /**
     * 获取所有所在部门和归属部门-包含已删除的
     * @param $unit_id
     * @param $departmentIds
     * @return array
     */
    public function getNotDelDepartmentAffiliationAll2($unit_id,$departmentIds): array
    {
        $DepartmentModel = new NotDelDepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid,name')->where(['unit_id' => $unit_id])->select()->toArray();
        $DepartmentData = [];
        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            $DepartmentData[$item['id']] = $item;
            $DepartmentData[$item['id']]['pids'][] = $item['id'];
            $DepartmentData[$item['id']]['ascription_name'] = '';
            $DepartmentData[$item['id']]['all_name'] = $item['name'];
            if ($item['pid'] == 0) continue;
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        if (in_array($item['id'], $departmentIds)) {
                            $departmentIds[] = $items['id'];
                        }
                        $DepartmentData[$item['id']]['pids'][] = $items['id'];
                        $DepartmentData[$item['id']]['ascription_name'] = $items['name'];
                        $DepartmentData[$item['id']]['all_name'] = $items['name'].'_'.$DepartmentData[$item['id']]['all_name'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        $departmentIds = array_unique($departmentIds);
        return [
            'department_data' => $DepartmentData,
            'department_ids' => $departmentIds
        ];
    }

    /**
     * 获取所有所在部门和归属部门
     * @param $unit_id
     * @param $departmentIds
     * @return array
     */
    public function getDepartmentAffiliationAll2($unit_id,$departmentIds): array
    {
        $DepartmentModel = new DepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid,name')->where(['unit_id' => $unit_id])->select()->toArray();
        $DepartmentData = [];
        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            $DepartmentData[$item['id']] = $item;
            $DepartmentData[$item['id']]['pids'][] = $item['id'];
            $DepartmentData[$item['id']]['ascription_name'] = '';
            $DepartmentData[$item['id']]['all_name'] = $item['name'];
            if ($item['pid'] == 0) continue;
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        if (in_array($item['id'], $departmentIds)) {
                            $departmentIds[] = $items['id'];
                        }
                        $DepartmentData[$item['id']]['pids'][] = $items['id'];
                        $DepartmentData[$item['id']]['ascription_name'] = $items['name'];
                        $DepartmentData[$item['id']]['all_name'] = $items['name'].'_'.$DepartmentData[$item['id']]['all_name'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        $departmentIds = array_unique($departmentIds);
        return [
            'department_data' => $DepartmentData,
            'department_ids' => $departmentIds
        ];
    }

    /**
     * 获取所有所在部门和归属部门-包含删除
     * @param $unit_id
     * @return array
     */
    public function getNotDelDepartmentAffiliationAll($unit_id): array
    {
        $DepartmentModel = new NotDelDepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid,name')->where(['unit_id' => $unit_id])->select()->toArray();
        $DepartmentData = [];
        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            $DepartmentData[$item['id']] = $item;
            $DepartmentData[$item['id']]['ascription_name'] = '';
            $DepartmentData[$item['id']]['all_name'] = $item['name'];
            if ($item['pid'] == 0) continue;
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        $DepartmentData[$item['id']]['ascription_name'] = $items['name'];
                        $DepartmentData[$item['id']]['all_name'] = $items['name'].'_'.$DepartmentData[$item['id']]['all_name'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        return $DepartmentData;
    }

    /**
     * 获取所有所在部门和归属部门
     * @param $unit_id
     * @return array
     */
   public function getDepartmentAffiliationAll($unit_id): array
   {
        $DepartmentModel = new DepartmentModel();
        $DepartmentList = $DepartmentModel->field('id,pid,name')->where(['unit_id' => $unit_id])->select()->toArray();
        $DepartmentData = [];
        foreach ($DepartmentList as $item) {
            $pid = $item['pid'];
            $DepartmentData[$item['id']] = $item;
            $DepartmentData[$item['id']]['ascription_name'] = '';
            $DepartmentData[$item['id']]['all_name'] = $item['name'];
            if ($item['pid'] == 0) continue;
            $i = 0;
            while ($pid > 0){
                if ($i > 10) break;//防止死循环
                foreach ($DepartmentList as $items){
                    if($pid == $items['id']){
                        $DepartmentData[$item['id']]['ascription_name'] = $items['name'];
                        $DepartmentData[$item['id']]['all_name'] = $items['name'].'_'.$DepartmentData[$item['id']]['all_name'];
                        $pid = $items['pid'];
                    }
                }
                $i++;
            }
        }
        return $DepartmentData;
   }

    /**
     * 获取所有部门管理员
     * @param $department_ids
     * @return array
     */
   public function getDepartmentAdminAll($department_ids): array
   {
        $DepartmentAdminModel = new DepartmentAdminModel();
        $DepartmentAdminList = $DepartmentAdminModel->field('uid,department_id')->where('department_id','in', $department_ids)
            ->with(['userName'])->select()->toArray();
        $DepartmentAdminData = [];
        foreach ($DepartmentAdminList as $item) {
            $DepartmentAdminData[$item['department_id']]['list'][] = $item;
            if (!empty($DepartmentAdminData[$item['department_id']]['user_name'])) {
                $DepartmentAdminData[$item['department_id']]['user_name'] .= '、'.$item['user_name'];
            }else{
                $DepartmentAdminData[$item['department_id']]['user_name'] = $item['user_name'];
            }
        }
        return $DepartmentAdminData;
   }

    /**
     * 获取所有政治面貌
     * @return array[]
     */
    public function getPoliticalOutlookSelector(): array
    {
        return [
            ['key'=>0,'value'=>'不限'],
            ['key'=>1,'value'=>'中共党员'],
            ['key'=>2,'value'=>'中共预备党员'],
            ['key'=>3,'value'=>'共青团员'],
            ['key'=>4,'value'=>'民革党员'],
            ['key'=>5,'value'=>'民盟盟员'],
            ['key'=>6,'value'=>'不限'],
            ['key'=>7,'value'=>'不限'],
            ['key'=>8,'value'=>'不限'],
            ['key'=>9,'value'=>'不限'],
            ['key'=>10,'value'=>'致公党党员'],
            ['key'=>11,'value'=>'九三学社社员'],
            ['key'=>12,'value'=>'无党派人士'],
            ['key'=>13,'value'=>'群众（现称普通居民）'],
            ['key'=>14,'value'=>'其他'],
        ];
    }

    /**
     * 获取所有婚姻状况
     * @return array[]
     */
    public function getMaritalStatusSelector(): array
    {
        return [
            ['key'=>0,'value'=>'不限'],
            ['key'=>1,'value'=>'未婚'],
            ['key'=>2,'value'=>'已婚'],
            ['key'=>3,'value'=>'离异'],
        ];
    }

    /**
     * 获取所有学籍状态
     * @return array[]
     */
    public function getStudentStatusSelector(): array
   {
       return [
           ['key'=>0,'value'=>'不限'],
           ['key'=>1,'value'=>'在读'],
           ['key'=>2,'value'=>'已毕业'],
           ['key'=>3,'value'=>'已退学'],
           ['key'=>4,'value'=>'休学'],
       ];
   }

    /**
     * 获取所有民族选择器
     * @return array[]
     */
    public function getNationSelector(): array
    {
        return [
            ['key'=>0,'value'=>'不限'],['key'=>1,'value'=>'汉族'], ['key'=>2,'value'=>'壮族'], ['key'=>3,'value'=>'回族'],
            ['key'=>4,'value'=>'藏族'], ['key'=>5,'value'=>'维吾尔族'], ['key'=>6,'value'=>'苗族'], ['key'=>7,'value'=>'彝族'],
            ['key'=>8,'value'=>'蒙古族'], ['key'=>9,'value'=>'布依族'], ['key'=>10,'value'=>'朝鲜族'], ['key'=>11,'value'=>'满族'],
            ['key'=>12,'value'=>'侗族'], ['key'=>13,'value'=>'瑶族'], ['key'=>14,'value'=>'白族'], ['key'=>15,'value'=>'土家族'],
            ['key'=>16,'value'=>'哈尼族'], ['key'=>17,'value'=>'哈萨克族'], ['key'=>18,'value'=>'傣族'], ['key'=>19,'value'=>'黎族'],
            ['key'=>20,'value'=>'傈僳族'], ['key'=>21,'value'=>'佤族'], ['key'=>22,'value'=>'畲族'], ['key'=>23,'value'=>'高山族'],
            ['key'=>24,'value'=>'拉祜族'], ['key'=>25,'value'=>'水族'], ['key'=>26,'value'=>'东乡族'], ['key'=>27,'value'=>'纳西族'],
            ['key'=>28,'value'=>'景颇族'], ['key'=>29,'value'=>'柯尔克孜族'], ['key'=>30,'value'=>'土族'],['key'=>31,'value'=>'达斡尔族'],
            ['key'=>32,'value'=>'仫佬族'], ['key'=>33,'value'=>'羌族'], ['key'=>34,'value'=>'布朗族'],['key'=>35,'value'=>'撒拉族'],
            ['key'=>36,'value'=>'毛南族'], ['key'=>37,'value'=>'仡佬族'], ['key'=>38,'value'=>'锡伯族'],['key'=>39,'value'=>'阿昌族'],
            ['key'=>40,'value'=>'普米族'], ['key'=>41,'value'=>'塔吉克族'], ['key'=>42,'value'=>'怒族'],['key'=>43,'value'=>'乌孜别克族'],
            ['key'=>44,'value'=>'俄罗斯族'], ['key'=>45,'value'=>'鄂温克族'], ['key'=>46,'value'=>'崩龙族(得昂族)'],['key'=>47,'value'=>'保安族'],
            ['key'=>48,'value'=>'裕固族'], ['key'=>49,'value'=>'京族'], ['key'=>50,'value'=>'塔塔尔族'],['key'=>51,'value'=>'独龙族'],
            ['key'=>52,'value'=>'鄂伦春族'], ['key'=>53,'value'=>'赫哲族'], ['key'=>54,'value'=>'门巴族'],['key'=>55,'value'=>'珞巴族'],['key'=>56,'value'=>'基诺族'],
        ];
    }

    /**
     * 获取残疾类型
     * @return string[]
     */
    public function getDisabilityType(): array
    {
        return ['视力残疾','听力残疾','言语残疾','肢体残疾','智力残疾','精神残疾','多重残疾',];
    }
}